/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessions;

import entities.NotificationType;
import entities.Playlist;
import entities.UUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Stateless
@LocalBean
public class PlaylistManager {
    
    @PersistenceContext(unitName = "ProjetStreamingBoulinguezCheng-ejbPU")
    private EntityManager em;
    @EJB
    private NotificationManager notificationManager;

    // Create a playlist if it doesn't exist yet in the DB - each playlist must have a unique title
    public boolean createPlayList(UUser owner, String title, String description, boolean privatePlaylist) {
        Playlist playlist = getPlaylist(owner, title);

        if (playlist == null) {
            // Get the date and create the playlist
            Date date = new Date();
            playlist = new Playlist(title, owner, date, description, privatePlaylist);
            persist(playlist);

            // Put the playlist into the user's playlist list
            owner.addPlaylist(playlist);
            update(owner);

            return true;
        }

        return false;
    }

    public boolean editPlaylist(UUser owner, Playlist playlist, String title, String description, boolean privatePlaylist) {
        // If the user modified the playlist title (orginal title != entered title)
        if (!playlist.getTitle().equals(title)) {
            Playlist existPlaylist = getPlaylist(owner, title);
            
            if (existPlaylist != null) {
                return false;
            }
        }

        playlist = em.merge(playlist);
        playlist.setTitle(title);
        playlist.setDescription(description);
        playlist.setPrivatePlaylist(privatePlaylist);
        update(playlist);

        return true;
    }

    // Get a playlist by it id
    public Playlist getPlaylist(long id) {
        Query query = em.createNamedQuery("Playlist.getById");
        query.setParameter("id", id);

        try {
            Object result = query.getSingleResult();
            return (Playlist) result;
        } catch (NoResultException e) {
            return null;
        }
    }

    // Get a playlist by it owner and title
    public Playlist getPlaylist(UUser owner, String title) {
        Query query = em.createNamedQuery("Playlist.getByOwnerAndTitle");
        query.setParameter("owner", owner);
        query.setParameter("title", title);

        try {
            Object result = query.getSingleResult();
            return (Playlist) result;
        } catch (NoResultException e) {
            return null;
        }
    }
    
    // Get public playlists
    public List<Playlist> getPublicPlaylists() {
        Query query = em.createNamedQuery("Playlist.getPublicPlaylists");
        return query.getResultList();
    }

    // Remove a playlist
    public void remove(Playlist playlist, UUser user) {
        // Merge then remove playlist
        user = em.merge(user);
        playlist = em.merge(playlist);
        user.removePlaylist(playlist);
        remove(playlist);
    }
    
    public void incPlaylistPlayCount(Long playlistId) {
        Playlist playlist = getPlaylist(playlistId);
        playlist.incPlayCount();
        update(playlist);
    }
    
    public void sharePlaylist(Playlist selectedPlaylist, UUser userFrom, UUser userTo) {
        // Add shared user to the playlist
        selectedPlaylist.addSharedUser(userTo);
        update(selectedPlaylist);
        
        // Add shared playlist to the user
        userTo.addSharedPlaylist(selectedPlaylist);
        update(userTo);
        
        // Create the notification
        String description = notificationManager.getPlaylistNotificationDescription(userFrom, selectedPlaylist.getTitle());
        notificationManager.createNotification(description, NotificationType.SHAREDPLAYLIST, userFrom, userTo);
    }

    public void remove(Object object) {
        em.remove(object);
    }

    public void persist(Object object) {
        em.persist(object);
    }

    public void update(Object object) {
        em.merge(object);
    }
}
